package com.gitee.dbswitch.admin.model.request;

import com.gitee.dbswitch.admin.common.exception.DbswitchException;
import com.gitee.dbswitch.admin.common.response.ResultCode;
import com.gitee.dbswitch.admin.entity.APIAssignmentConfigEntity;
import com.gitee.dbswitch.admin.entity.AssignmentConfigEntity;
import com.gitee.dbswitch.admin.entity.AssignmentTaskEntity;
import com.gitee.dbswitch.admin.type.ScheduleModeEnum;
import com.gitee.dbswitch.admin.util.CronExprUtils;
import com.gitee.dbswitch.common.util.PatterNameUtils;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Objects;

@NoArgsConstructor
@Data
public class APITaskCreateRequest extends APITaskBaseRequest {

    private String name;
    private String description;
    private ScheduleModeEnum scheduleMode;
    private String cronExpression;
    private APITaskConfig config;

    public AssignmentTaskEntity toAPITask() {
        AssignmentTaskEntity assignment = new AssignmentTaskEntity();
        assignment.setId(null);
        assignment.setName(name);
        assignment.setDescription(description);
        assignment.setScheduleMode(scheduleMode);
        if (ScheduleModeEnum.SYSTEM_SCHEDULED == this.getScheduleMode()) {
            CronExprUtils.checkCronExpressionValid(this.getCronExpression(), CronExprUtils.MIN_INTERVAL_SECONDS);
            assignment.setCronExpression(this.getCronExpression());
        }

        return assignment;
    }

    public APIAssignmentConfigEntity toAPIConfig(Long assignmentId) {

        APIAssignmentConfigEntity apiAssignmentConfigEntity = toAPIConfig(assignmentId, config);
        apiAssignmentConfigEntity.setFirstFlag(Boolean.TRUE);

        return apiAssignmentConfigEntity;
    }
}
